// pages/mine/record/daily/index.js
const app = getApp()
import {formatDate} from '../../../../utils/util'
Page({
   

    data: {
    // pet 下拉框
             // 下拉框选中数据
        choosedPetCondition: {
            nickname: '选择宠物', 
            id: ''
        },
        conditionPetVisible: false,
        conditionPetList: [{
            nickname: '选择所有',
            id:'0'
        }], // 下拉框数据

    // 记录type 下拉框
        choosedTypeCondition: {
            type: '选择类型', 
            id: ''
        },
        conditionTypeVisible: false,
        conditionTypeList: [{
                type:'选择所有',
                id:'0',
            },{
                type:'清洁记录',
                id:'1'
            },{
                type:'喂食记录',
                id:'2',
            },{
                type:'花费记录',
                id:'3',
            }
        ], // 下拉框数据

    // 日期选择器
        tempDateBegin:formatDate(new Date()),
        tempDateEnd:formatDate(new Date()),
        timeNow: formatDate(new Date()),

    //   卡片
        bannerCurrent: 0, // 当前显示的banner, 即petDailyInfo的索引
        notUpdate:true,
        // petDailyInfos
        petDailyInfos:[{
            id:'0',
            petNickname:'没有符合要求的记录',
        }],
        
    // update 表格数据
        tempFormData:'',
        formConditionTypeVisible:'',
        formConditionTypeList: [{
            type:'清洁记录',
            id:'0',
        },{
            type:'喂食记录',
            id:'1'
        },{
            type:'花费记录',
            id:'2'
        }
         ],

    // 添加页面
        show: false,
        duration: 300,
        addTime: formatDate(new Date()),
        conditionAddPetVisible:false,
        conditionAddTypeVisible:false,
        conditionAddPetList: '',
        choosedAddTypeCondition: {
            type: '选择类型', 
            id: '-1',
        },
        choosedAddPetCondition: {
            nickname: '选择宠物', 
            id: '-1`',
        },

    },

    onLoad(e){
        var that = this
        that.initializeDate()
    },

    search: function () {
        var that = this
        let tempNickname = that.data.choosedPetCondition.id == 0 ? "" : that.data.choosedPetCondition.nickname
        let tempType = that.data.choosedTypeCondition.id == 0 ? "" : that.data.choosedTypeCondition.type
          wx.request({
            url: app.globalData.prefixUrl + '/chong/record/daily/query',
            data:{
                userId: app.globalData.userId,
                type: tempType,
                petNickname: tempNickname,
                beginTime: that.data.tempDateBegin,
                endTime: that.data.tempDateEnd
            },
            method:"GET",
            success(e) {
              if (e.data != null && e.data != '') {
                that.setData({
                    petDailyInfos: e.data,
                    notUpdate: true,
                    show: false,
                    bannerCurrent: 0,
                })
              } else {
              that.setData({
                'petDailyInfos[0].petNickname': '没有符合要求的记录',
                'petDailyInfos[0].id':'0',
                'petDailyInfos[0].petPhoto':'',
                 notUpdate: true,
                 show: false,
                 bannerCurrent: 0,
              })
            }
            },
            fail(e){
                console.log("获取dailyInfos失败")
            }
          })
        
    },

    // 添加
    popAddContainer(){
        // 弹出page-container
        let duration = this.data.duration
        this.setData({
            show: true,
            duration
        })
    },

    showPetCondition() {
        this.setData({
            conditionPetVisible: !this.data.conditionPetVisible
        })
    },
    showTypeCondition() {
        this.setData({
            conditionTypeVisible: !this.data.conditionTypeVisible
        })
    },
    formShowTypeCondition() {
        this.setData({
            formConditionTypeVisible: !this.data.formConditionTypeVisible
        })
    },
    showAddPetCondition() {
        this.setData({
            conditionAddPetVisible: !this.data.conditionAddPetVisible
        })
    },
    showAddTypeCondition() {
        this.setData({
            conditionAddTypeVisible: !this.data.conditionAddTypeVisible
        })
    },

    // 改变查询项
    onPetChnageCondition(e) {
        const list = this.data.conditionPetList
        list.forEach(item => {
            if (item.id === e.currentTarget.dataset.id) {
                item.select = true
                this.setData({
                    'choosedPetCondition.nickname': item.nickname,
                    'choosedPetCondition.id': item.id
                })
            } else {
                item.select = false
            }
        })
        if (e.currentTarget.dataset.id == 0) {
            this.setData({
                'choosedPetCondition.nickname': '选择所有宠物',
                'choosedPetCondition.id': '0'
            })
        }
        this.setData({
            conditionPetList: list
        })
    },
    onTypeChnageCondition(e) {
        const list = this.data.conditionTypeList
        list.forEach(item => {
            if (item.id === e.currentTarget.dataset.id) {
                item.select = true
                this.setData({
                    'choosedTypeCondition.type': item.type,
                    'choosedTypeCondition.id': item.id
                })
            } else {
                item.select = false
            }
        })
        if (e.currentTarget.dataset.id == 0) {
            this.setData({
                'choosedTypeCondition.type': '选择所有类型',
                'choosedTypeCondition.id': '0'
            })
        }
        this.setData({
            conditionTypeList: list
        })
    },
    onAddPetChnageCondition(e){
        const list = this.data.conditionAddPetList
        list.forEach(item => {
            if (item.id === e.currentTarget.dataset.id) {
                item.select = true
                this.setData({
                    'choosedAddPetCondition.nickname': item.nickname,
                    'choosedAddPetCondition.id': item.id,
                    'choosedAddPetCondition.petPhoto': item.petPhoto
                })
            } else {
                item.select = false
            }
        })
        this.setData({
            conditionAddPetList: list
        })
    },
    onAddTypeChnageCondition(e) {
        const list = this.data.formConditionTypeList
        list.forEach(item => {
            if (item.id === e.currentTarget.dataset.id) {
                item.select = true
                this.setData({
                    'choosedAddTypeCondition.type': item.type,
                    'choosedAddTypeCondition.id': item.id
                })
            } else {
                item.select = false
            }
        })
        this.setData({
            formConditionTypeList: list
        })
    },
    
    // date picker
    changeDateBegin(e){
        var that = this
        console.log("当前Begin选中时间: ", e.detail.value)
        that.setData({ 
          'tempDateBegin':e.detail.value,
      });
      },
      
    changeDateEnd(e){
        var that = this
        console.log("当前End选中时间: ", e.detail.value)
        that.setData({ 
          'tempDateEnd':e.detail.value,
      });
    },

    changeDate(e){
        var that = this
        console.log("当前编辑选中时间: ", e.detail.value)
        that.setData({ 
            addTime: e.detail.value,
      });
    },

    formChangeData(e){
        var that = this
        console.log("当前编辑选中时间: ", e.detail.value)
        that.setData({ 
          'tempFormData.time':e.detail.value,
      });
    },
    // 卡片滑动+翻转
  // 滑动
  bannerSwiper(e) {
    const that = this, 
    bannerCurrent = e.detail.current;
    that.setData({
      bannerCurrent,
    })
    console.log("当前bannder: "+bannerCurrent+"\t是否是正面: "+that.data.notUpdate)
  },

  // 删除
  delRecordInfo(){
    var that = this
     wx.showModal({
        title: '提示',
        content: '确定删除该记录吗?',
        cancelText: '取消',
        confirmText: '确认',
        success: res => {
          if (res.confirm) {
            wx.request({
                url: app.globalData.prefixUrl + '/chong/record/daily/' + that.data.petDailyInfos[that.data.bannerCurrent].id,
                method:"DELETE",
                success(e){
                  console.log("记录 "+that.data.petDailyInfos[that.data.bannerCurrent].id+ " 删除成功")
                  let petDailyInfos = that.data.petDailyInfos
                  petDailyInfos.splice(that.data.bannerCurrent, 1)
                  that.setData({
                      petDailyInfos
                  })
                  if (petDailyInfos.length == 0) {
                    that.setData({
                        'petDailyInfos[0].petNickname': '没有符合要求的记录',
                        'petDailyInfos[0].id':'0',
                      })
                  }
                },
                fail(e){
                  console.log("记录 "+that.data.petDailyInfos[that.data.bannerCurrent].id+ " 删除失败")
                }
              })
          }
        }
      })
    
  },
  
  // 编辑
  updateRecord(){
      var that  = this
        let formData = that.data.petDailyInfos[that.data.bannerCurrent]
      that.setData({
        notUpdate: false,
        tempFormData: formData
      })
  },

  formOnTypeChnageCondition(e){
    const list = this.data.formConditionTypeList
    list.forEach(item => {
        if (item.id === e.currentTarget.dataset.id) {
            item.select = true
            this.setData({
                'tempFormData.type': item.type,
            })
        } else {
            item.select = false
        }
    })
    this.setData({
        formConditionTypeList: list
    })
  },

  // 保存编辑
  saveUpdate(e){
    var that = this
    let tempFormData = that.data.tempFormData
    let formData=e.detail.value;
    wx.request({
      url: app.globalData.prefixUrl + '/chong/record/daily/' + tempFormData.id,
      data:{
          type: tempFormData.type,
          name:formData.name,
          description: formData.description,
          price: formData.price,
          time:tempFormData.time
      },
      method:"PUT",
      header:{
        'Content-type': "application/x-www-form-urlencoded"
      },
      success(e){

          console.log("record:"+tempFormData.id+"数据更新成功. ", e)
          that.search()
      }
    })
  },
  // 取消编辑
  cancelUpdate(){
      var that = this
      that.setData({
          notUpdate: true,
      })
  },

  // 保存添加
  saveAdd(e){
    var that = this
    
    let formData=e.detail.value;
    let pet = that.data.choosedAddPetCondition
    let type = that.data.choosedAddTypeCondition.type
    // 判空, 不想做

    wx.request({
      url: app.globalData.prefixUrl +'/chong/record/daily/',
      data:{
        userId: app.globalData.userId,
        petId: pet.id,
        petNickname: pet.nickname,
        petPhoto: pet.petPhoto,
        type: type,
        name: formData.name,
        description: formData.description,
        price: formData.price == null ? -1 : formData.price,
        time: that.data.addTime
      },
      method:"POST",
      header:{
        'Content-type': "application/x-www-form-urlencoded"
      },
      success(e){
          console.log("添加记录成功: ",e)
          that.search()
      },
    })

    that.setData({
        show: false
    })

  },


  initializeDate(){
    var that = this

    // 用户宠物数据, 下拉框
    wx.request({
      url: app.globalData.prefixUrl + '/chong/pet/pet/user/' + app.globalData.userId,
      method:"GET",
      success(e){
        console.log("获取当前用户宠物数据成功: ",e)
        that.setData({
            conditionPetList: that.data.conditionPetList.concat(e.data),
            conditionAddPetList: e.data
        })
      },
      fail(e){
        console.log("获取当前用户宠物数据失败: ",e)
      }
    })

    // 当前日期的宠物所有记录
    wx.request({
        url: app.globalData.prefixUrl + '/chong/record/daily/query',
        data:{
            userId: app.globalData.userId,
            beginTime: that.data.timeNow,
            endTime: that.data.timeNow,
        },
        method:"GET",
        success(e) {
            if (e.data != null && e.data != '') {
              that.setData({
                  petDailyInfos: e.data,
                  tempFormData: e.data[0],
              })
            } else {
            that.setData({
              'petDailyInfos[0].petNickname': '没有符合要求的记录',
              'petDailyInfos[0].id':'0',
            })
        }
         
        },
        fail(e){
            console.log("获取当前日期宠物的所有记录失败: ", e.data)
        },
    })
},

})